package com.orangelife.mobile.point.activity;

import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.common.sp.CommSp2;
import com.common.sp.UserSp;
import com.common.util.OrangePhoneUtil;
import com.curry.android.util.IntentHelper;
import com.curry.android.util.StringUtil;
import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.common.activity.OrangeLifeBaseActivity;
import com.orangelife.mobile.util.ToastHelper;

/**
 * 积分攻略
 *
 * @author lxj
 */
public class PointGuideActivity extends OrangeLifeBaseActivity {

    /** 积分攻略文本内容 */
    private TextView tvTitle, tvTitleScore, tvScoreStrategy;
    /** 积分 */
    private LinearLayout llScore;

    @Override
    protected void onCreate(Bundle paramBundle) {
        super.onCreate(paramBundle);
        setContentView(R.layout.activity_individual_score_strategy);
        initView();
        findView();
    }

    /** 初始化组件 */
    private void initView() {
        tvTitle = (TextView) findViewById(R.id.tv_title);
        tvTitleScore = (TextView) findViewById(R.id.tv_title_score);
        tvScoreStrategy = (TextView) findViewById(R.id.tvScoreStrategy);
        llScore = (LinearLayout) findViewById(R.id.ll_score);
    }

    /** 实例化组件 */
    private void findView() {
        tvTitle.setText(getResources().getString(R.string.jifen_gonglve));
        tvTitleScore.setText(UserSp.getPoint());
        llScore.setOnClickListener(CommonOnClickListener);
        final String phone = CommSp2.getCustomerServicePhone();
        ClickableSpan clickableSpan = new ClickableSpan() {
            @Override
            public void onClick(View view) {
                OrangePhoneUtil.callCustomerService2(PointGuideActivity.this);
            }

            @Override
            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(getResources().getColor(R.color.common_blue));
                ds.setUnderlineText(true);
                ds.clearShadowLayer();
            }
        };

        SpannableString spannableString = new SpannableString(tvScoreStrategy.getText().toString() + phone);
        int size = tvScoreStrategy.length();
        spannableString.setSpan(clickableSpan, size, size + phone.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        tvScoreStrategy.setText(spannableString);
        tvScoreStrategy.setMovementMethod(LinkMovementMethod.getInstance());
    }

    /** 监听器——根据id判断并执行相应操作 */
    View.OnClickListener CommonOnClickListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.ll_score:
                    if (!StringUtil.isContainBlank(UserSp.getLoginID())) {
                        IntentHelper.getInstances().getIntent(PointGuideActivity.this,
                                MyPointActivity.class);
                    } else {
                        ToastHelper.getInstance()._toast("未登录无法访问积分");
                    }
                    break;
                default:
                    break;
            }
        }
    };
}
